package com.niuquan.activitie;

import android.app.Activity;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.mobileim.IYWLoginService;
import com.alibaba.mobileim.YWAPI;
import com.alibaba.mobileim.YWIMKit;
import com.alibaba.mobileim.YWLoginParam;
import com.alibaba.mobileim.channel.event.IWxCallback;
import com.alibaba.sdk.android.AlibabaSDK;
import com.alibaba.sdk.android.callback.InitResultCallback;
import com.alibaba.tcms.env.YWEnvManager;
import com.alibaba.tcms.env.YWEnvType;

public class ChenlinTestActivity extends Activity {

    private TextView name;
    private TextView id;
    private YWIMKit mIMKit;
    private ImageView image;
    private Handler handle=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            // TODO Auto-generated method stub
            if (msg.what == 0x100) {
                Toast.makeText(ChenlinTestActivity.this, "成功",
                        Toast.LENGTH_SHORT).show();
                Intent intent =mIMKit.getConversationActivityIntent();
                startActivity(intent);
            }
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chenlin_test);
        Button b = (Button) findViewById(R.id.button);
        Button a = (Button) findViewById(R.id.button5);
        Button friend = (Button) findViewById(R.id.button7);
        name=(TextView) findViewById(R.id.textView2);
        id = (TextView) findViewById(R.id.textView3);
        image = (ImageView) findViewById(R.id.imageView);
        Button c = (Button) findViewById(R.id.button6);
        Button register = (Button) findViewById(R.id.button8);
        ExpertCircle appState=(ExpertCircle) getApplicationContext();
        final String APP_KEY = "23173937";
        //第一个参数是Application Context
        //这里的APP_KEY即应用创建时申请的APP_KEY
        YWEnvManager.prepare(this, YWEnvType.ONLINE);
        YWAPI.init(appState, APP_KEY);
        AlibabaSDK.asyncInit(this, new InitResultCallback() {

            @Override
            public void onSuccess() {
                Toast.makeText(ChenlinTestActivity.this, "初始化成功", Toast.LENGTH_SHORT)
                        .show();
            }

            @Override
            public void onFailure(int code, String message) {
                Toast.makeText(ChenlinTestActivity.this, "初始化异常", Toast.LENGTH_SHORT)
                        .show();
            }

        });

        c.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mIMKit = YWAPI.getIMKitInstance();
                String userId = "small";
                String password = "123456";
                IYWLoginService loginService = mIMKit.getLoginService();
                YWLoginParam loginParam = YWLoginParam.createLoginParam(userId, password);
                loginService.login(loginParam, new IWxCallback() {

                    @Override
                    public void onSuccess(Object... arg0) {
                        handle.sendEmptyMessage(0x100);
                    }

                    @Override
                    public void onProgress(int arg0) {
                        // TODO Auto-generated method stub
                    }

                    @Override
                    public void onError(int errCode, String description) {
                        //如果登录失败，errCode为错误码,description是错误的具体描述信息
                        System.out.println(errCode);
                        System.out.println(description);
                    }
                });

            }
        });

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_chenlin_test, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
